package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
        Person p=new Person();
        p.sayHello();
        Class cls = Class.forName("reflect.Person");
        Object obj=cls.newInstance();

        Method method=cls.getMethod("sayHello");
        method.invoke(obj);

        Scanner scan=new Scanner(System.in);
        System.out.println("请输入类名：");
        String className=scan.nextLine();
        System.out.println("请输入一个方法名");
        String methodName=scan.nextLine();

        Class cls2=Class.forName(className);
        Object obj2=cls2.newInstance();
        Method method2=cls2.getMethod(methodName);
        method2.invoke(obj2);
    }
}